#ifndef __HT_H_
#define __HT_H_

typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;

/*dangerous macro, don't call it with instant return val from function call*/
#define max(a,b) (((a)<(b))?(b):(a))
#define min(a,b) (((a)>(b))?(b):(a))

#define SHUT_FD(fd) do {                          \
                    if(fd>=0)                     \
		      {                           \
			shutdown(fd, SHUT_RDWR);  \
			close(fd);                \
			fd = -1;                  \
		      }                           \
                    } while(0)

#define HT_SUCCESS 0
#define HT_VERSION_NOT_SUPPORTED -1
#define HT_AUTH_NOT_SUPPORTED -2
#define HT_ADDR_NOT_SUPPORTED -3
#define HT_DNS_FAIL -4
#define HT_CONNECT_FAIL -5
#define HT_NODATA -6

#define HTCON_STAT_INIT 0
#define HTCON_STAT_CLIENT_CONNECTED 1
#define HTCON_STAT_CLIENT_AUTHING 2
#define HTCON_STAT_CLIENT_AUTHED 3
#define HTCON_STAT_CLIENT_READY 4
#define HTCON_STAT_READY 5
#define HTCON_STAT_CLOSING 127

#define HT_CFD_READ    0x00000001
#define HT_CFD_WRITE   0x00000002
#define HT_CFD_OOB     0x00000004
#define HT_SFD_READ    0x00000008
#define HT_SFD_WRITE   0x00000010
#define HT_SFD_OOB     0x00000020

#define HT_CLOSE_CLIENT 1
#define HT_CLOSE_SERVER 2

#define HTCON_BUF_SIZE 2048

struct htcon
{
  char caddr[16];
  char saddr[16];
  unsigned short cport;
  unsigned short sport;
  int status;
  int sfd;
  int cfd;
  int bufsize;
  int cswpos;         /*client->server buf write position*/
  int csrpos;         /*client->server buf read position*/
  int scwpos;         /*server->client buf write position*/
  int scrpos;         /*server->client buf read position*/
  uchar * upbuf;       /*client->server buf*/
  uchar * downbuf;     /*server->client buf*/
  char coob;
  char soob;
  int coobflg;
  int soobflg;
  struct htcon * pre;
  struct htcon * next;
};

  
extern int htlisten(char * ip, unsigned short port);


#endif /*__HT_H_*/
